from dataclasses import dataclass
from typing import List

import requests
import json


@dataclass
class NormalItem:
    mongodbId: str
    createTime: str
    location: str
    modelSize: str


@dataclass
class Data:
    error: List[str]
    normal: List[NormalItem]


@dataclass
class Response:
    success: bool
    data: Data
    status: str
    message: str


def send_sniffing_request(archive, standard_time, body):
    # 构建请求的 URL
    url = f"{archive}/api/service/archive/bizData/sniffingodsmodeltime"
    # 设置请求参数
    params = {
        "standardTime": standard_time
    }
    # 将 body 参数转换为 JSON 格式
    payload = json.dumps(body)
    # 设置请求头
    headers = {
        "Content-Type": "application/json"  # 指定请求体为 JSON 格式
    }
    # 发送 POST 请求
    response = requests.post(url, headers=headers, params=params, data=payload)
    # 检查响应状态
    if response.status_code == 200:
        return response.json()  # 返回 JSON 格式的响应内容
    else:
        response.raise_for_status()  # 抛出异常以处理错误响应


# 示例调用
archive = "https://dcost-sub-pre.glodon.com"  # 替换为实际的 archive URL
# 问题工程的开始时间
standard_time = "2024-10-15 12:00:00"
body = [
    "6629ecf8fd1b2500079364b8",
    "661204701193d50008e75ed0",
    "662757be8fe94e00077c16e3",
    "65e80cbabdc5b50007195b36",
    "6639e88befd1720007becfe2",
    "662f79519a6d59000746f4a8",
    "656eff916b9b420007e01d0c",
    "662f7945efd1720007be5b49",
    "662f79409a6d59000746f4a6",
    "662f77c59a6d59000746f485",
    "662f769cefd1720007be5b25",
    "662f75a7efd1720007be5b11",
    "662f7467988fad000777ab0e",
    "662f74649a6d59000746f457",
    "662f74abefd1720007be5b01",
    "662f741defd1720007be5a74",
    "662f73d5988fad000777aaf6",
    "662f7380efd1720007be5a6c",
    "66275404848afb0007def6c8",
    "66166b0d1193d50008e7b371",
    "660cdb5baaf0b200071c9acb",
    "660cdb0ca9c1030008e7ac42",
    "660cd9ccaaf0b200071c9abc",
    "660cd89caaf0b200071c9aaf",
    "660cd878aaf0b200071c9aad",
    "660cd7e6aaf0b200071c9aa0",
    "660cd7c2a9c1030008e7ac1f",
    "660b5fc29ff8a90007463af6",
    "66084be6f08d230007af5034",
    "6608450c3c9ce00007ae305a",
    "66084507f08d230007af4ff0",
    "660814d8feeb1d0008f92f21",
    "6606b848feeb1d00076f4521",
    "65fbd6f87dc2480007f1fa1b",
    "65fbd6997dc2480007f1fa17",
    "65fbd68a7dc2480007f1fa16",
    "65fbd6847dc2480007f1fa15",
    "65fbd67c28d22d0008765d9a",
    "65fbd6709cbf030007444d9e",
    "65fbd66328d22d0008765d98",
    "65f4442f7dc24800071a1064",
    "65f443b07dc24800071a1063",
    "65f432c47dc24800071a105f",
    "65ba5b9d93a4a60007899295",
    "65968de07441a10007da41de",
    "65ba4331e85ac200074bb7a9",
    "65ba4328e85ac200074bb7a8",
    "65ba4324e85ac200074bb7a7",
    "65ba43208109a2000731dfda",
    "65ba3f2b93a4a6000789928c",
    "65ba3f1893a4a6000789928a",
    "65ba3e9693a4a60007899289",
    "65ba3dcde85ac200074bb7a6",
    "65ba3c3893a4a60007899288",
    "65ba26a693a4a60007899273",
    "65b9b617c1f50b0007fe7c45",
    "65968dda6b9b4200074a68e9",
    "65968c657441a10007da41dd",
    "658c14926b9b420007150731",
    "658aa4b56b9b4200071505d5",
    "658973d77a14a40007206652",
    "658964de603cef0008720fac",
    "6579987983954b00076b20a1",
    "657996db39d73800071d41c2",
    "657996d4b259ae0008706094",
    "6579928883954b00076b209f",
    "657838546b9b420007aea7fd",
    "657837106b9b420007aea7fc",
    "6578370d6b9b420007aea7fb",
    "656e787ada29130007f55a86",
    "656de110da29130007f55a7a",
    "656de0db6b9b420007e01bab",
    "656ddfb56b9b420007e01ba9",
    "656dddfe6b9b420007e01ba7",
    "656dd98ada29130007f55a75",
    "656dd8466b9b420007e01ba5",
    "656dd83dda29130007f55a73",
    "656d99f0330e85000783aac2",
    "656d9981da29130007f55a69",
    "656d98a1da29130007f55a67",
    "65647da527ea3100077154ab",
    "65647bd727ea3100077154a9",
    "6543a96fec8a1800077ec5c4",
    "6543a67cd11c6a00088f1e79",
    "6543a4c73b6d310007105ba6",
    "663c6212efd1720007bf0910",
    "661386ac75639c000783145a"
]


def from_json_to_normal(json_str: str):
    data_dict = json.loads(json_str)
    normal_items = [NormalItem(**item) for item in data_dict['data']['normal']]
    # noinspection PyArgumentList data = Data(error=data_dict['data']['error'], normal=normal_items) response =
    # Response(success=data_dict['success'], data=data, status=data_dict['status'],  message=data_dict['message'])
    return normal_items


try:
    response_data = send_sniffing_request(archive, standard_time, body)
    for normal in from_json_to_normal(json.dumps(response_data)):
        # 使用 f-string 格式化字符串
        formatted_str = f"""
                db.project.updateOne(
                      {{ _id: {{ $in: [ObjectId("{normal.mongodbId}")] }} }},
                      {{ $set: {{ location: '{normal.location}', uploadBizDataStatus: true }} }}
                );
        """
        print(formatted_str)
except Exception as e:
    print("Error:", str(e))
